package com.sakura.mall.product.spuImages.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.product.spuImages.domain.entity.SpuImagesEntity;
import com.sakura.mall.product.spuImages.domain.request.AddSpuImagesRequest;
import com.sakura.mall.product.spuImages.domain.request.UpdateSpuImagesRequest;
import com.sakura.mall.product.spuImages.domain.request.SpuImagesPageRequest;
import com.sakura.mall.product.spuImages.domain.vo.SpuImagesVo;
import com.sakura.mall.product.spuImages.mapper.SpuImagesMapper;
import com.sakura.mall.product.spuImages.service.SpuImagesService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * spu图片 service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class SpuImagesServiceImpl extends ServiceImpl<SpuImagesMapper, SpuImagesEntity> implements SpuImagesService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddSpuImagesRequest request) {
        // todo 其他校验逻辑
        SpuImagesEntity spuImages = request.convert(SpuImagesEntity.class);
        this.save(spuImages);
    }

    @Override
    public Page<SpuImagesVo> queryPage(SpuImagesPageRequest request) {
        IPage<SpuImagesEntity> qp = request.toPage();
        LambdaQueryWrapper<SpuImagesEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<SpuImagesVo> ret = qp.convert(entity -> entity.convert(SpuImagesVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdateSpuImagesRequest request) {
        SpuImagesEntity entity = request.convert(SpuImagesEntity.class);
        entity.setId(id);
        this.updateById(entity);
    }

}

